﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;

namespace BranchOfSansar.Register.Products
{
    public partial class frmProduct : Form
    {
        public Product SelectedItem;
        public frmProduct()
        {
            InitializeComponent();
        }

        private void BindProduct()
        {
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                gridProduct.DataSource = reg.ProductList();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmRegProduct frm = new frmRegProduct();
            frm.ShowDialog();
            BindProduct();

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (viewProduct.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }
            Product prod = (Product)viewProduct.GetRow(viewProduct.GetSelectedRows()[0]);
            frmRegProduct frm = new frmRegProduct(prod);
            frm.ShowDialog(this);
            BindProduct();
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {
            BindProduct();
        }

        private void gridProduct_DoubleClick(object sender, EventArgs e)
        {
            if (viewProduct.SelectedRowsCount > 0)
            {
                SelectedItem = (Product)viewProduct.GetRow(viewProduct.GetSelectedRows()[0]);
                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (viewProduct.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }
            if (Globals.Question("Бүтээгдэхүүнийг устгах уу?") == System.Windows.Forms.DialogResult.No)
                return;
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                reg.DeleteProduct((Product)viewProduct.GetRow(viewProduct.GetSelectedRows()[0]));
            }
            BindProduct();
        }
    }
}
